package com.koi.common.utils.file;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ･ᴗ･
 * @description [TextUtil]
 */
@Slf4j
public class TextUtil {

    /**
     * @param filePath
     * @description [流方式读取txt文本数据]
     * @author ･ᴗ･
     */
    public static List<String> readText(String filePath) {
        System.out.println("文件路径：" + filePath);

        BufferedReader br = null;
        List<String> dataArray = new ArrayList();
        String line = null;
        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            String str = "";

            //循环读取文件的每一行，对需要修改的行进行修改，放入缓冲对象中
            while ((line = br.readLine()) != null) {
                //设置正则将多余空格都转为一个空格
                str = line;
                dataArray.add(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {// 关闭流
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                }
            }
        }
        return dataArray;
    }

    /**
     * @param filePath 文件路径 + 文件名称
     * @description [创建文件]
     * @author ･ᴗ･
     */
    public static void createFile(String filePath) {
        try {
            File file = new File(filePath);

            if (!file.exists()) {
                file.getParentFile().mkdirs();
                try {
                    file.createNewFile();
                    System.out.println("File created successfully.");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("File already exists.");
            }
        } catch (Exception e) {
            log.info("创建文件失败：{}", e.getMessage());
        }
    }

    /**
     * @description [一行一行写txt文件内容]
     * @author ･ᴗ･
     */
    public static void writeRowText(String filePath, String content) {
        /* 一行一行写txt文件追加内容 */
        try {
            FileWriter writer = new FileWriter(filePath, true);  // 创建文件写入器，参数为要写入的文件路径，第二个参数为true表示以追加模式写入
            String[] splitArray = content.split("/n");
            for (String rowSplit : splitArray) {
                writer.write(rowSplit);  // 按行写入文本数据，每行末尾加上换行符
            }
            writer.close();
        } catch (Exception e) {
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }


    public static void main(String[] args) {
        /* 读txt文件内容 */
        // String path = "C:\\Users\\yuxin\\Desktop\\server日志\\16各server日志\\jr43.log.2023-10-23-11";
        // List<String> list = readText(path);
        // for (String string : list) {
        //     System.out.println("数据：" + string);
        // }

        /* 一行一行写txt文件内容 */
        String filePath = "D:\\testFile\\koiTest.txt";
        try {
            createFile(filePath);
            writeRowText(filePath, "TEST1\nTEST2\nTEST3\n");
            writeRowText(filePath, "TEST4\nTEST5\nTEST6\n");
        } catch (Exception e) {
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }
}
